package com.nunbey.lingxi.lincat.datastore;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

public class SharedPreferencesHelper {
    public void saveData(Context context,String fileName,String key,Object value){
        try {
            SharedPreferences sharedPreferences = context.getSharedPreferences(fileName,Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(key,value.toString());
            editor.apply();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    public void saveData(Activity activity,String key,Object value){
        try {
            SharedPreferences sharedPreferences = activity.getPreferences(Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(key,value.toString());
            editor.apply();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public String getData(Context context,String fileName,String key){
        try {
            SharedPreferences sharedPreferences = context.getSharedPreferences(fileName,Context.MODE_PRIVATE);
            return sharedPreferences.getString(key,"defaultValue");
        }
        catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public String getData(Activity activity,String key){
        try {
            SharedPreferences sharedPreferences =activity.getPreferences(Context.MODE_PRIVATE);
            return sharedPreferences.getString(key,"defaultValue");
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }



}
